package api

import (
	"net/http"
	"strconv"

	// "GinAdmin/internal/model"
	// "GinAdmin/internal/service"
	"GinAdmin/model"
	"GinAdmin/service"

	"github.com/gin-gonic/gin"
)

// MessageAPI 消息API
type MessageAPI struct {
	messageService service.MessageService
}

// CreateMessage 创建消息
// @Summary 创建消息
// @Description 创建新消息
// @Tags 消息管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body model.CreateMessageRequest true "消息信息"
// @Success 200 {object} model.Response
// @Router /message [post]
func (a *MessageAPI) CreateMessage(c *gin.Context) {
	var req model.CreateMessageRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "请求参数错误: " + err.Error(),
		})
		return
	}

	// 获取当前用户ID
	userID, _ := c.Get("userID")
	req.UserID = userID.(uint)

	message, err := a.messageService.CreateMessage(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "发送消息失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "发送成功",
		Data: message,
	})
}

// ReadMessage 读取消息
// @Summary 读取消息
// @Description 标记消息为已读
// @Tags 消息管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "消息ID"
// @Success 200 {object} model.Response
// @Router /message/read/{id} [put]
func (a *MessageAPI) ReadMessage(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "无效的消息ID",
		})
		return
	}

	// 获取当前用户ID
	userID, _ := c.Get("userID")

	if err := a.messageService.ReadMessage(uint(id), userID.(uint)); err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "读取消息失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "已读成功",
	})
}

// DeleteMessage 删除消息
// @Summary 删除消息
// @Description 删除指定消息
// @Tags 消息管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "消息ID"
// @Success 200 {object} model.Response
// @Router /message/{id} [delete]
func (a *MessageAPI) DeleteMessage(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: 400,
			Msg:  "无效的消息ID",
		})
		return
	}

	// 获取当前用户ID
	userID, _ := c.Get("userID")

	if err := a.messageService.DeleteMessage(uint(id), userID.(uint)); err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "删除消息失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "删除成功",
	})
}

// GetUserMessages 获取用户消息
// @Summary 获取用户消息
// @Description 获取当前用户的消息列表
// @Tags 消息管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param page query int false "页码"
// @Param page_size query int false "每页大小"
// @Param status query int false "状态(0:未读,1:已读,-1:全部)"
// @Success 200 {object} model.PageResponse
// @Router /message/list [get]
func (a *MessageAPI) GetUserMessages(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	status, _ := strconv.Atoi(c.DefaultQuery("status", "-1"))

	// 获取当前用户ID
	userID, _ := c.Get("userID")

	messages, total, err := a.messageService.GetUserMessages(userID.(uint), page, pageSize, status)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取消息列表失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.PageResponse{
		Code:  200,
		Msg:   "获取成功",
		Data:  messages,
		Total: total,
		Page:  page,
		Size:  pageSize,
	})
}

// GetUnreadCount 获取未读消息数量
// @Summary 获取未读消息数量
// @Description 获取当前用户的未读消息数量
// @Tags 消息管理
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.Response
// @Router /message/unread/count [get]
func (a *MessageAPI) GetUnreadCount(c *gin.Context) {
	// 获取当前用户ID
	userID, _ := c.Get("userID")

	count, err := a.messageService.GetUnreadCount(userID.(uint))
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: 500,
			Msg:  "获取未读消息数量失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 200,
		Msg:  "获取成功",
		Data: count,
	})
}
